package com.explorex.infra.puma.gateway.netty.ws.enums;

import lombok.Getter;

/**
 * @author: JacceYang
 * @createTime: 2022/07/15 10:21 上午
 * @description: 消息包类型
 */

@Getter
public enum MessageTypeEnum {
    PING(0x00, "PING"),
    PONG(0x80, "PONG"),

    AUTH(0x01, "AUTH"),
    AUTH_ACK(0x81, "AUTH_ACK"),

    CLOSE_SOCKET(0x02, "CLOSE_SOCKET"),
    CLOSE_SOCKET_ACK(0x82, "CLOSE_SOCKET_ACK"),

    /**
     * 客户端上行的消息
     */
    MESSAGE(0x03, "MESSAGE"),
    MESSAGE_ACK(0x83, "MESSAGE_ACK"),

    /**
     * 服务端下行的消息
     */
    MESSAGE_DOWN(0x04, "MESSAGE_DOWN"),
    MESSAGE_DOWN_ACK(0x84, "MESSAGE_DOWN_ACK"),

    BIND(0x05, "BIND"),
    BIND_ACK(0x85, "BIND_ACK"),

    STATUS_CHANGE(0x06, "STATUS_CHANGE"),
    STATUS_CHANGE_ACK(0x86, "STATUS_CHANGE_ACK"),
    /**
     * 加密协商
     */
    HAND_SHAKE(0x07, "HAND_SHAKE"),
    HAND_SHAKE_ACK(0x87, "HAND_SHAKE_ACK"),

    VIRTUAL_LINK(0x08, "VIRTUAL_LINK"),
    VIRTUAL_LINK_ACK(0x88, "VIRTUAL_LINK_ACK"),

    REGISTER_EVENT(0x09, "REGISTER_EVENT"),
    REGISTER_EVENT_ACK(0x99, "REGISTER_EVENT_ACK"),

    SILENCE(0x8d, "Silence"),
    ERROR(0x8e, "Error"),
    UNKNOWN(0x8f, "UNKNOWN");

    private final short type;
    private final String desc;


    MessageTypeEnum(int type, String desc) {
        this.type = (short) type;
        this.desc = desc;
    }

    public static MessageTypeEnum findByType(short type) {
        for (MessageTypeEnum value : MessageTypeEnum.values()) {
            if (value.getType() == type) {
                return value;
            }
        }
        return UNKNOWN;
    }

}
